package com.huika.renrench.control.my.activity;

import java.util.ArrayList;
import java.util.Iterator;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.huika.lib.utils.MoneyShowTool;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.my.adapter.TransactionRecordAdapter;
import com.huika.renrench.control.my.adapter.TransactionRecordGridViewAdapter;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.TransactionRecordBean;
import com.huika.renrench.view.TitleBarHelper;

/**
 * 
 * @description：交易记录
 * @author zc
 * @date 2015年6月2日 上午11:33:25
 */
public class TransactionRecordActivity extends BaseAct implements Listener<RequestResult<ArrayList<TransactionRecordBean>>>, OnItemClickListener, OnRefreshListener2<ListView> {

	private TextView all;
	private TextView classify;
	private PullToRefreshListView listview;
	private PopupWindow popupWindow;
	private View layout;
	private int pageNo = 1;
	private int pageSize = 10;
	private ArrayList<TransactionRecordBean> nullList = new ArrayList<TransactionRecordBean>();//空
	private ArrayList<TransactionRecordBean> allList = new ArrayList<TransactionRecordBean>();//全部
	private ArrayList<TransactionRecordBean> recharList = new ArrayList<TransactionRecordBean>();//充值
	private ArrayList<TransactionRecordBean> withDrawList = new ArrayList<TransactionRecordBean>();//提现
	private ArrayList<TransactionRecordBean> investList = new ArrayList<TransactionRecordBean>();//投资
	private ArrayList<TransactionRecordBean> investEarList = new ArrayList<TransactionRecordBean>();//投资收益
	private ArrayList<TransactionRecordBean> bondTransferList = new ArrayList<TransactionRecordBean>();//债券转让
	private ArrayList<TransactionRecordBean> transferPoundageList = new ArrayList<TransactionRecordBean>();//转让扣款
	private int type = 0;
	private GridView gridView;
	private boolean isUpRefresh;
	private TransactionRecordAdapter adapter;
	private String userId;
	private View classfy_layout;
	private ImageView icon;
	private View line;
	private ArrayList<String> list;
	private TransactionRecordGridViewAdapter gridViewAdapter;
	private View my_view;
	
	@Override
	public void setRootView() {
		setContentView(R.layout.activity_transaction_record);
	}
	
	@Override
	protected void initWidget() {
		super.initWidget();
		all = (TextView) findViewById(R.id.all);
		classify = (TextView) findViewById(R.id.classify);
		listview = (PullToRefreshListView) findViewById(R.id.listview);
		layout = findViewById(R.id.layout);
		classfy_layout = findViewById(R.id.classfy_layout);
		icon = (ImageView) findViewById(R.id.icon);
		line = findViewById(R.id.line);
		my_view = findViewById(R.id.my_view);
		all.setOnClickListener(this);
		classify.setOnClickListener(this);
		adapter = new TransactionRecordAdapter(this);
		classfy_layout.setOnClickListener(this);
		icon.setOnClickListener(this);
		listview.setAdapter(adapter);
		listview.setOnRefreshListener(this);
		listview.setOnItemClickListener(new ListOnItemClickListener());
		all.performClick();
		getTransactionList(userId,type,pageNo,pageSize);
		
		
	}
	
	private void getData() {
		if(list == null) {
			list = new ArrayList<String>();
			list.add("充值");
			list.add("提现");
			list.add("投资");
			list.add("投资收益");
			list.add("债权转让");
			list.add("转让扣款");
		}
	}
	
	@Override
	protected void initData() {
		super.initData();
		TitleBarHelper barHelper = new TitleBarHelper(this, -1);
		barHelper.setLeftMsg("我的");
		barHelper.setTitleMsg("交易记录");
		getData();
		userId = GlobalApp.userModel.getUserId();
	}
	
	private void popWindow() {
		if(popupWindow == null) {
			gridView = (GridView) inflater.inflate(R.layout.classfy_gridview, null);
			gridViewAdapter = new TransactionRecordGridViewAdapter(this);
			popupWindow = new PopupWindow(gridView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
			int[] location = new int[2];
			all.getLocationOnScreen(location);
			x = location[0];
			y = location[1];
			height = all.getHeight();
			gridView.setAdapter(gridViewAdapter);
			gridViewAdapter.setGroup(list);
			ColorDrawable dw = new ColorDrawable(0xb0000000);
			popupWindow.setBackgroundDrawable(dw);
			gridView.setOnItemClickListener(this);
			popupWindow.setOnDismissListener(new OnDismissListener() {
				
				@Override
				public void onDismiss() {
					icon.setImageResource(R.drawable.transfer_record_down_press);
					my_view.setVisibility(View.GONE);
					if(type == 0) {
						all.performClick();
					}
				}
			});
		}
		popupWindow.showAtLocation(layout, Gravity.NO_GRAVITY, 0, (y + height));
		icon.setImageResource(R.drawable.transfer_record_up_press);
		my_view.setVisibility(View.VISIBLE);
	}
	
	private int x;
	private int y;
	private int height;
	
	/**
	 * 
	 * @description：type（0..全部1.充值2.提现3.投资4.投资收益5.债权转让）
	 * @author zc
	 * @date 2015年6月3日 下午2:32:35
	 */
	private void getTransactionList(String userId, int typeId, int pageNo, int pageSize) {
		showLoadingDialog(getString(R.string.common_loading));
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putCommonTypeParam("userId", userId);
		ajaxParams.putCommonTypeParam("typeId", typeId);
		ajaxParams.putCommonTypeParam("pageNo", pageNo);
		ajaxParams.putCommonTypeParam("pageSize", pageSize);
		FormResultRequest<ArrayList<TransactionRecordBean>> request = new FormResultRequest<ArrayList<TransactionRecordBean>>(UrlConstants.GETTRANSACTIONLIST, this, this, new TypeToken<RequestResult<ArrayList<TransactionRecordBean>>>() {
		}.getType());
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}

	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
			case R.id.all:
				all.setSelected(true);
				classfy_layout.setSelected(false);
				classify.setSelected(false);
				line.setBackgroundColor(getResources().getColor(R.color.white));
				icon.setImageResource(R.drawable.transfer_record_down_normal);
				if(type != 0) {//当前选择不是全部点击全部才进行加载
					type = 0 ;
					getTransactionList(userId,type,pageNo,pageSize);
				}
				classify.setText("分类");
				break;
			case R.id.classify:
			case R.id.classfy_layout:
			case R.id.icon:
				classfy_layout.setSelected(true);
				classify.setSelected(true);
				line.setBackgroundColor(getResources().getColor(R.color.color_38bdee));
				all.setSelected(false);
				if(popupWindow != null && popupWindow.isShowing()) {
					popupWindow.dismiss();
				} else {
					popWindow();
				}
				break;

		}
	}

	@Override
	public void onResponse(RequestResult<ArrayList<TransactionRecordBean>> requestResult) {
		dismissLoadingDialog();
		listview.onRefreshComplete();
		if(requestResult.flag == 1) {
			if(requestResult.getRs() != null && requestResult.getRs().size() == 10) {
				listview.setMode(Mode.BOTH);
			} else {
				listview.setMode(Mode.PULL_FROM_START);
			}
			if(requestResult.getRs() != null && requestResult.getRs().size() > 0) {
				requestResult.setRs(getNewList(requestResult.getRs()));
				if(isUpRefresh) {
					switch (type) {
						case 0:
							allList.addAll(requestResult.getRs());
							adapter.setGroup(allList);
							break;
						case 1:
							recharList.addAll(requestResult.getRs());
							adapter.setGroup(recharList);
							break;
						case 2:
							withDrawList.addAll(requestResult.getRs());
							adapter.setGroup(withDrawList);
							break;
						case 3:
							investList.addAll(requestResult.getRs());
							adapter.setGroup(investList);
							break;
						case 4:
							investEarList.addAll(requestResult.getRs());
							adapter.setGroup(investEarList);
							break;
						case 5:
							bondTransferList.addAll(requestResult.getRs());
							adapter.setGroup(bondTransferList);
							break;
						case 7:
							transferPoundageList.addAll(requestResult.getRs());
							adapter.setGroup(transferPoundageList);
							break;
					}
				} else {
					switch (type) {
						case 0:
							allList = requestResult.getRs();
							adapter.setGroup(allList);
							break;
						case 1:
							recharList = requestResult.getRs();
							adapter.setGroup(recharList);
							break;
						case 2:
							withDrawList = requestResult.getRs();
							adapter.setGroup(withDrawList);
							break;
						case 3:
							investList = requestResult.getRs();
							adapter.setGroup(investList);
							break;
						case 4:
							investEarList = requestResult.getRs();
							adapter.setGroup(investEarList);
							break;
						case 5:
							bondTransferList = requestResult.getRs();
							adapter.setGroup(bondTransferList);
							break;
						case 7:
							transferPoundageList = requestResult.getRs();
							adapter.setGroup(transferPoundageList);
							break;
					}
				}
			} else {
				/**
				 * modify by :zwy
				 */
//				adapter.setGroup(nullList);
			}
			
		}
		isUpRefresh = false;
	}
	
	private ArrayList<TransactionRecordBean> getNewList(ArrayList<TransactionRecordBean> list) {
		Iterator<TransactionRecordBean> iterator = list.iterator();
		while(iterator.hasNext()) {
			if(MoneyShowTool.twolastDFStr(iterator.next().getAmount()).equals("0.00")) {
				iterator.remove();
			}
		}
		return list;
	}
	
	class ListOnItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
			Intent intent = new Intent(TransactionRecordActivity.this, TransactionDetailActivity.class);
			intent.putExtra("recordBean", adapter.getItem(position - 1));
			startActivity(intent);
		}
		
	}
	
	@Override
	public void onErrorResponse(VolleyError error) {
		super.onErrorResponse(error);
		listview.onRefreshComplete();
		if(isUpRefresh) {
			pageNo--;
		}
		isUpRefresh = false;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		type = position + 1;
		if(type > 5) {//中间没有6的情况
			type++;
		}
		pageNo = 1;
		classify.setText(gridViewAdapter.getItem(position));
		/**
		 * modify by:zwy
		 */
		adapter.setGroup(nullList);
		getTransactionList(userId, type, pageNo, pageSize);
		popupWindow.dismiss();
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> arg0) {
		pageNo = 1;
		getTransactionList(userId,type,pageNo,pageSize);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> arg0) {
		isUpRefresh = true;
		getTransactionList(userId,type,++pageNo,pageSize);
	}

}
